﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STS.Schema.Interfaces;
using System.Collections;
using STS.Interfaces;
using STS.Schema.Enums;

namespace STS.Schema.Base
{
    public class SqlObjectCollection<T> : List<T>, ISqlObjectCollection, IList<T>, ICollection<T>, IEnumerable<T>
        where T : ISqlObject
    {
        public ISqlObject Parent
        {
            get;
            protected set;
        }

        public TSqlObject GetObject<TSqlObject>(string name, IDatabase database = null) where TSqlObject : ISqlObject
        {
            return this.OfType<TSqlObject>().FirstOrDefault(item => item.Name == name);
        }

        public TSqlObject GetObject<TSqlObject>(int index, IDatabase database = null) where TSqlObject : ISqlObject
        {
            return this.OfType<TSqlObject>().FirstOrDefault();
        }

        public SqlObjectCollection(ISqlObject parent)
            : this(parent, null)
        {

        }

        public SqlObjectCollection(ISqlObject parent, IEnumerable<T> collection)
        {
            Parent = parent;
            if (collection != null)
            {
                base.AddRange(collection);
            }
        }

        public virtual void Refresh()
        {
            base.Clear();
        }

        public virtual string GetCollectionQuery(SqlObjectType? sqlObjectType = null, IDatabase database = null)
        {
            return null;
        }
    }
}
